package top.xia17.plugins.fast.quartz.api.controller;

import top.xia17.plugins.fast.common.model.Back;
import top.xia17.plugins.fast.common.model.PageData;
import top.xia17.plugins.fast.common.model.PageParam;
import top.xia17.plugins.fast.common.database.jpa.JpaUtil;
import top.xia17.plugins.fast.common.utils.VerifyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import top.xia17.plugins.fast.quartz.api.entity.QuartzLog;
import top.xia17.plugins.fast.quartz.api.query.QuartzLogQuery;
import top.xia17.plugins.fast.quartz.api.service.QuartzLogService;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * quartz定时任务的执行日志接口
 *
 * @author xia17
 * @date 2022-06-13 17:51:48
 */
@RestController
@RequestMapping("quartzLog")
@RequiredArgsConstructor
public class QuartzLogController {

    private final QuartzLogService quartzLogService;

    /**
     * 新增quartz定时任务的执行日志
     *
     * @param resource 请求参数
     * @return 返回新增的数据
     */
    @PostMapping("create")
    public Back<QuartzLog> create(@RequestBody @Valid QuartzLog resource) {
        QuartzLog entity = quartzLogService.create(resource);
        return Back.ok(entity);
    }


    /**
     * 修改quartz定时任务的执行日志
     *
     * @param resource 请求参数
     * @return 返回修改后的数据
     */
    @PostMapping("update")
    public Back<QuartzLog> update(@RequestBody @Valid QuartzLog resource) {
        QuartzLog entity = quartzLogService.update(resource);
        return Back.ok(entity);
    }

    /**
     * 删除单个quartz定时任务的执行日志
     *
     * @param id quartz定时任务的执行日志的id
     * @return /
     */
    @PostMapping
    public Back<String> delete(@NotNull Long id) {
        VerifyUtil.notNull(id, "参数异常");
        quartzLogService.deleteById(id);
        return Back.ok();
    }

    /**
     * 删除多个quartz定时任务的执行日志
     *
     * @param ids quartz定时任务的执行日志的id
     * @return /
     */
    @PostMapping("delete_by_ids")
    public Back<String> deleteByIds(@NotNull @RequestBody List<Long> ids) {
        quartzLogService.deleteById(ids);
        return Back.ok();
    }


    /**
     * quartz定时任务的执行日志分页列表
     *
     * @param query     查询条件
     * @param pageParam 分页参数
     * @return /
     */
    @GetMapping("page")
    public Back<PageData<QuartzLog>> page(QuartzLogQuery query, @Valid PageParam pageParam) {
        Page<QuartzLog> page = quartzLogService.findAll(query, pageParam.toPageable());
        return Back.ok(JpaUtil.page(page));
    }


    /**
     * quartz定时任务的执行日志列表
     *
     * @param query 查询条件
     * @return /
     */
    @GetMapping("list")
    public Back<List<QuartzLog>> list(QuartzLogQuery query) {
        List<QuartzLog> list = quartzLogService.findAll(query);
        return Back.ok(list);
    }

}

